﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using ServiceStack.Redis;
using ServiceManager.Utils;

namespace QuotationService.RealtimeQuotation
{
    /// <summary>
    /// 历史分线通道
    /// </summary>
    [DebuggerNonUserCode]
    internal static class MinuteDataChannel
    {
        /// <summary>
        /// 保存历史分线信息
        /// </summary>
        /// <param name="source"></param>
        /// <param name="code"></param>
        /// <param name="minute"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static bool Update(QuotationSource source, string code, Int32 minute, byte[] data)
        {
            if (source == null)
                throw new ArgumentNullException("source");

            if (string.IsNullOrEmpty(code))
                throw new ArgumentNullException("code");

            if (data == null || data.Length == 0)
                throw new ArgumentNullException("data");

            var publisher = QuotationPublishServiceContainer.GetPublishService(source);
            if (publisher == null)
                return false;

            using (var conn = publisher.GetNeedDisposedConnection())
            {
                using (var pipeline = conn.CreatePipeline())
                {
                    //更新分线历史
                    publisher.Update(source.ToFormatString() + ":" + QuotationDataType.MinuteDataList.ToFormatString() + ":" + code, minute.ToString("d4"), data, pipeline);

                    //更新最新分线
                    publisher.Update(source.ToFormatString() + ":" + QuotationDataType.MinuteData.ToFormatString(), code, data, pipeline);

                    pipeline.Flush();

                    return true;
                }
            }
        }

        /// <summary>
        /// 获取所有商品的最新分线数据
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static List<byte[]> GetAll(QuotationSource source)
        {
            if (source == null)
                throw new ArgumentNullException("source");

            using (var conn = QuotationSubscribeServiceContainer.GetNeedDisposedConnection(source))
            {
                if (conn == null)
                    return null;

                var retValues = (conn as RedisClient).HVals(source.ToFormatString() + ":" + QuotationDataType.MinuteData.ToFormatString());
                if (retValues == null)
                    return null;

                return retValues.ToList();
            }
        }

        /// <summary>
        /// 删除所有商品的最新分线数据
        /// </summary>
        /// <param name="source"></param>
        public static bool ClearAll(QuotationSource source)
        {
            if (source == null)
                throw new ArgumentNullException("source");

            var publisher = QuotationPublishServiceContainer.GetPublishService(source);
            if (publisher == null)
                return false;

            //删除所有商品的最新分线
            if (!publisher.Delete(source.ToFormatString() + ":" + QuotationDataType.MinuteData.ToFormatString()))
                return false;

            //删除所有商品的历史分线
            var keys = publisher.SearchKeys(source.ToFormatString() + ":" + QuotationDataType.MinuteDataList.ToFormatString() + ":*").ToArray();
            if (keys.Length > 0)
                return publisher.Delete(keys);

            return true;
        }

        /// <summary>
        /// 获取单只商品的最新分线数据
        /// </summary>
        /// <param name="source"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static byte[] Get(QuotationSource source, string code)
        {
            if (source == null)
                throw new ArgumentNullException("source");

            if (string.IsNullOrEmpty(code))
                throw new ArgumentNullException("code");

            using (var conn = QuotationSubscribeServiceContainer.GetNeedDisposedConnection(source))
            {
                if (conn == null)
                    return null;

                return (conn as RedisClient).HGet(source.ToFormatString() + ":" + QuotationDataType.MinuteData.ToFormatString(), code.ToUtf8Bytes());
            }
        }

        /// <summary>
        /// 删除单只商品的最新分线数据
        /// </summary>
        /// <param name="source"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static bool Clear(QuotationSource source, string code)
        {
            if (source == null)
                throw new ArgumentNullException("source");

            if (string.IsNullOrEmpty(code))
                throw new ArgumentNullException("code");

            var publisher = QuotationPublishServiceContainer.GetPublishService(source);
            if (publisher == null)
                return false;

            return publisher.Delete(source.ToFormatString() + ":" + QuotationDataType.MinuteData.ToFormatString(), code);
        }

        /// <summary>
        /// 获取单只商品的历史行情
        /// </summary>
        /// <param name="source"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static List<byte[]> GetAll(QuotationSource source, string code)
        {
            if (source == null)
                throw new ArgumentNullException("source");

            if (string.IsNullOrEmpty(code))
                throw new ArgumentNullException("code");

            using (var conn = QuotationSubscribeServiceContainer.GetNeedDisposedConnection(source))
            {
                if (conn == null)
                    return null;

                var retValues = (conn as RedisClient).HVals(source.ToFormatString() + ":" + QuotationDataType.MinuteDataList.ToFormatString() + ":" + code);
                if (retValues == null)
                    return null;

                return retValues.ToList();
            }
        }

        /// <summary>
        /// 删除单只商品的历史分线
        /// </summary>
        /// <param name="source"></param>
        /// <param name="code"></param>
        public static bool ClearAll(QuotationSource source, string code)
        {
            if (source == null)
                throw new ArgumentNullException("source");

            if (string.IsNullOrEmpty(code))
                throw new ArgumentNullException("code");

            var publisher = QuotationPublishServiceContainer.GetPublishService(source);
            if (publisher == null)
                return false;

            return publisher.Delete(source.ToFormatString() + ":" + QuotationDataType.MinuteDataList.ToFormatString() + ":" + code);
        }
    }
}
